/**
 * Layout Store
 * 
 * Manages layout preferences and presets
 */

import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export interface LayoutPreset {
  id: string;
  name: string;
  sidebarWidth: number;
  sessionPanelWidth: number;
  sidebarCollapsed: boolean;
  sessionPanelCollapsed: boolean;
}

export const LAYOUT_PRESETS: LayoutPreset[] = [
  {
    id: 'compact',
    name: '紧凑',
    sidebarWidth: 150,
    sessionPanelWidth: 200,
    sidebarCollapsed: false,
    sessionPanelCollapsed: false,
  },
  {
    id: 'default',
    name: '默认',
    sidebarWidth: 200,
    sessionPanelWidth: 250,
    sidebarCollapsed: false,
    sessionPanelCollapsed: false,
  },
  {
    id: 'wide',
    name: '宽屏',
    sidebarWidth: 250,
    sessionPanelWidth: 300,
    sidebarCollapsed: false,
    sessionPanelCollapsed: false,
  },
  {
    id: 'focus',
    name: '专注模式',
    sidebarWidth: 200,
    sessionPanelWidth: 250,
    sidebarCollapsed: true,
    sessionPanelCollapsed: true,
  },
];

interface LayoutStore {
  // State
  currentPreset: string;
  sidebarWidth: number;
  sessionPanelWidth: number;
  sidebarCollapsed: boolean;
  sessionPanelCollapsed: boolean;
  
  // Actions
  applyPreset: (presetId: string) => void;
  setSidebarWidth: (width: number) => void;
  setSessionPanelWidth: (width: number) => void;
  toggleSidebar: () => void;
  toggleSessionPanel: () => void;
  resetToDefaults: () => void;
}

export const useLayoutStore = create<LayoutStore>()(
  persist(
    (set) => ({
      // Initial state (default preset)
      currentPreset: 'default',
      sidebarWidth: 200,
      sessionPanelWidth: 250,
      sidebarCollapsed: false,
      sessionPanelCollapsed: false,

      // Apply a preset
      applyPreset: (presetId: string) => {
        const preset = LAYOUT_PRESETS.find(p => p.id === presetId);
        if (preset) {
          set({
            currentPreset: presetId,
            sidebarWidth: preset.sidebarWidth,
            sessionPanelWidth: preset.sessionPanelWidth,
            sidebarCollapsed: preset.sidebarCollapsed,
            sessionPanelCollapsed: preset.sessionPanelCollapsed,
          });
        }
      },

      // Update widths
      setSidebarWidth: (width: number) => set({ sidebarWidth: width }),
      setSessionPanelWidth: (width: number) => set({ sessionPanelWidth: width }),

      // Toggle panels
      toggleSidebar: () => set((state) => ({ 
        sidebarCollapsed: !state.sidebarCollapsed 
      })),
      toggleSessionPanel: () => set((state) => ({ 
        sessionPanelCollapsed: !state.sessionPanelCollapsed 
      })),

      // Reset to defaults
      resetToDefaults: () => {
        const defaultPreset = LAYOUT_PRESETS.find(p => p.id === 'default');
        if (defaultPreset) {
          set({
            currentPreset: 'default',
            sidebarWidth: defaultPreset.sidebarWidth,
            sessionPanelWidth: defaultPreset.sessionPanelWidth,
            sidebarCollapsed: defaultPreset.sidebarCollapsed,
            sessionPanelCollapsed: defaultPreset.sessionPanelCollapsed,
          });
        }
      },
    }),
    {
      name: 'layout-storage',
    }
  )
);
